<?php
namespace app\controller\admin;

use support\Request;
use app\service\OrderService;
use app\service\OrderRefundService;
use app\support\Response;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("订单管理")
 * @Apidoc\Group("admin")
 */
class OrderController
{
    protected $service;
    protected $refundService;

    public function __construct()
    {
        $this->service = new OrderService;
        $this->refundService = new OrderRefundService;
    }

    /**
     * @Apidoc\Title("获取订单列表")
     * @Apidoc\Method("GET")
     * @Apidoc\Url("/admin/order/index")
     * @Apidoc\Param("page", type="int", default=1, desc="页码")
     * @Apidoc\Param("page_size", type="int", default=15, desc="每页数量")
     * @Apidoc\Param("order_no", type="string", desc="订单编号")
     * @Apidoc\Param("status", type="int", desc="订单状态")
     * @Apidoc\Param("refund_status", type="int", desc="退款状态")
     * @Apidoc\Param("shipping_phone", type="string", desc="收货人电话")
     * @Apidoc\Param("start_time", type="string", desc="开始时间")
     * @Apidoc\Param("end_time", type="string", desc="结束时间")
     * @Apidoc\Returned("data", type="object", desc="分页数据", children={
     *   @Apidoc\Returned("items", type="array", desc="数据列表", children={
     *     @Apidoc\Returned("id", type="int", desc="ID"),
     *     @Apidoc\Returned("created_at", type="string", desc="创建时间"),
     *     @Apidoc\Returned("updated_at", type="string", desc="更新时间")
     *   }),
     *   @Apidoc\Returned("total", type="int", desc="总数"),
     *   @Apidoc\Returned("page", type="int", desc="当前页"),
     *   @Apidoc\Returned("page_size", type="int", desc="每页数量")
     * })
     */
    public function index(Request $request)
    {
        try {
            $params = $request->all();
            $list = $this->service->list($params);
            return Response::paginate($list);
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("获取订单详情")
     * @Apidoc\Method("GET")
     * @Apidoc\Url("/admin/order/show/{id}")
     * @Apidoc\Param("id", type="int", require=true, desc="订单ID")
     * @Apidoc\Returned("data", type="object", desc="详情数据", children={
     *   @Apidoc\Returned("id", type="int", desc="ID"),
     *   @Apidoc\Returned("created_at", type="string", desc="创建时间"),
     *   @Apidoc\Returned("updated_at", type="string", desc="更新时间")
     * })
     */
    public function show($id)
    {
        try {
            $result = $this->service->find($id);
            return Response::success($result);
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("订单发货")
     * @Apidoc\Method("POST")
     * @Apidoc\Url("/admin/order/ship/{id}")
     * @Apidoc\Param("id", type="int", require=true, desc="订单ID")
     * @Apidoc\ParamType("json")
     * @Apidoc\Param("shipping_company", type="string", require=true, desc="快递公司")
     * @Apidoc\Param("shipping_no", type="string", require=true, desc="快递单号")
     */
    public function ship(Request $request, $id)
    {
        try {
            $data = $request->post();
            $result = $this->service->ship($id, $data);
            return Response::success($result, '发货成功');
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("获取退款申请列表")
     * @Apidoc\Method("GET")
     * @Apidoc\Url("/admin/order/refund/list")
     * @Apidoc\Param("page", type="int", default=1, desc="页码")
     * @Apidoc\Param("page_size", type="int", default=15, desc="每页数量")
     * @Apidoc\Param("order_no", type="string", desc="订单编号")
     * @Apidoc\Param("refund_no", type="string", desc="退款编号")
     * @Apidoc\Param("status", type="int", desc="退款状态")
     * @Apidoc\Param("start_time", type="string", desc="开始时间")
     * @Apidoc\Param("end_time", type="string", desc="结束时间")
     */
    public function refundList(Request $request)
    {
        try {
            $params = $request->all();
            $list = $this->refundService->list($params);
            return Response::paginate($list);
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("获取退款申请详情")
     * @Apidoc\Method("GET")
     * @Apidoc\Url("/admin/order/refund/show/{id}")
     * @Apidoc\Param("id", type="int", require=true, desc="退款申请ID")
     */
    public function refundShow($id)
    {
        try {
            $result = $this->refundService->find($id);
            return Response::success($result);
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("同意退款申请")
     * @Apidoc\Method("POST")
     * @Apidoc\Url("/admin/order/refund/agree/{id}")
     * @Apidoc\Param("id", type="int", require=true, desc="退款申请ID")
     */
    public function refundAgree(Request $request, $id)
    {
        try {
            $admin = $request->admin;
            $result = $this->refundService->agree($id, $admin->id);
            return Response::success($result, '操作成功');
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("拒绝退款申请")
     * @Apidoc\Method("POST")
     * @Apidoc\Url("/admin/order/refund/refuse/{id}")
     * @Apidoc\Param("id", type="int", require=true, desc="退款申请ID")
     * @Apidoc\ParamType("json")
     * @Apidoc\Param("refuse_reason", type="string", require=true, desc="拒绝原因")
     */
    public function refundRefuse(Request $request, $id)
    {
        try {
            $data = $request->post();
            $admin = $request->admin;
            $result = $this->refundService->refuse($id, $data['refuse_reason'], $admin->id);
            return Response::success($result, '操作成功');
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("确认退款")
     * @Apidoc\Method("POST")
     * @Apidoc\Url("/admin/order/refund/confirm/{id}")
     * @Apidoc\Param("id", type="int", require=true, desc="退款申请ID")
     * @Apidoc\ParamType("json")
     * @Apidoc\Param("refund_trade_no", type="string", require=true, desc="退款交易号")
     */
    public function refundConfirm(Request $request, $id)
    {
        try {
            $data = $request->post();
            $admin = $request->admin;
            $result = $this->refundService->confirm($id, $data['refund_trade_no'], $admin->id);
            return Response::success($result, '退款成功');
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("退款统计")
     * @Apidoc\Method("GET")
     * @Apidoc\Url("/admin/order/refund/statistics")
     * @Apidoc\Param("start_time", type="string", desc="开始时间")
     * @Apidoc\Param("end_time", type="string", desc="结束时间")
     */
    public function refundStatistics(Request $request)
    {
        try {
            $params = $request->all();
            $result = $this->refundService->statistics($params);
            return Response::success($result);
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }
} 